How to use - Events
Event handler function: property value change
You can listen the value change event from any property in the vcPropertyContainer
When the OnChanged event happens, an event-handler function is called.
Use "+=" operator when assigning the event-handler function.
import vcCore as vc def my_prop_changed(prop): print(f"Property: {prop.Name}, new value: {prop.Value}") comp = vc.getComponent() prop_container = comp.Properties my_prop = prop_container["MyStringProperty"] Use reset() if the subscription can execute more than once, e.g., in a loop. my_prop.OnChanged.reset() # not needed here at script's main level. my_prop.OnChanged += my_prop_changed
Event inline waiting: property value change
Wait for a property to change.
""" property value change during the simulation. """ import vcCore as vc async def OnRun(): comp = vc.getComponent() my_prop = comp.Properties["MyIntProperty"] await my_prop.OnChanged.wait() print ("Property value changed")
Event inline waiting (with a filter): arriving product
Wait for product arrival to a container.
""" product arrival to a container. """ import vcCore as vc def filter_arriving(product, is_arriving): # filter is passed only when the filter function returns True return is_arriving async def OnRun(): comp = vc.getComponent() cont = comp.findBehavior("ComponentContainer") await cont.OnTransition.wait(filter_arriving) print ("Product arrived")
Advanced: event handler inside a custom class
See the example: Strong vs. Weak Reference